package com.compass.doctor.dialog

import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.databinding.DialogDrugLitsBinding
import com.compass.doctor.ui.prescription.adapter.PrescriptionDrugAdapter
import com.compass.framework.base.BaseDialog
import com.compass.framework.base.BaseDialogFragment
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.ext.onClick
import com.compass.framework.model.Drug
import com.compass.framework.toast.TipsToast
import com.compass.framework.utils.dpToPx

/**
 * Description： 药品信息list
 * Date:2024/3/27 17:01
 */
class DrugInfoDialog {

    class Builder(
        activity: FragmentActivity,
        private var addedDrugList: ArrayList<Drug>,
        private var onSubmitCallBack: OnCustomButtonClick?
    ) : BaseDialogFragment.Builder<Builder>(activity) {


        private lateinit var prescriptionDrugAdapter: PrescriptionDrugAdapter
        private val mBinding: DialogDrugLitsBinding =
            DialogDrugLitsBinding.inflate(LayoutInflater.from(activity))

        init {
            initView()
        }

        private fun initView() {
            setContentView(mBinding.root)
            setWidth(ViewGroup.LayoutParams.MATCH_PARENT)
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
            setAnimStyle(BaseDialog.AnimStyle.BOTTOM)
            gravity = Gravity.BOTTOM

            prescriptionDrugAdapter = PrescriptionDrugAdapter(2)
            val dp10 = dpToPx(10)
            mBinding.recyclerView.apply {
                layoutManager = LinearLayoutManager(context)
                addItemDecoration(NormalItemDecoration().apply {
                    setBounds(left = dp10, top = dp10, right = dp10, bottom = dp10)
                    setLastBottom(true)
                })
                adapter = prescriptionDrugAdapter
            }

            prescriptionDrugAdapter.setData(addedDrugList)
            setTips()
            prescriptionDrugAdapter.setCustomBottomClick(object : OnCustomButtonClick {
                override fun onButtonCallBack(position: Int, msgType: String?) {
                    when (msgType) {
                        "delete" -> {
                            addedDrugList.remove(addedDrugList[position])
                            prescriptionDrugAdapter.setData(addedDrugList)
                            setTips()
                            onSubmitCallBack?.onButtonCallBack(position,"delete")
                        }
                    }


                }
            })

            mBinding.tvCancel.onClick {
                dismiss()
            }


            mBinding.tvSubmit.onClick {
                if (addedDrugList.size==0){
                    TipsToast.showTips("请先添加药品")
                    return@onClick
                }
                onSubmitCallBack?.onButtonCallBack(0,"submit")
                dismiss()

            }
        }

        private fun setTips() {
            mBinding.tvDrugsNumber.text="已添加${addedDrugList.size}种药品"

        }


    }
}